// pages/houseDetail/houseDetails.js
import {ajax} from "../../utils/httpRequest.js";
import {URI_GETHOUSEDETAILS,URI_ADDHISTORY,URI_ADDCOLLECTION,URI_CANCELCOLLECTION,URI_ISCOLLECTION} from "../../api.js";

var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: {},
    bannerList: {},
    update_time: '',
    latAndLon: {},
    hide:'hide',
    show:false,
    is_no_collection:true,
    houseId:'',
    title:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.title
    });
    this.getHouseDetails(options.id);
    this.addHistory(options.id);
    this.isCollection(options.id);
    this.setData({
      houseId:options.id,
      title:options.title
    });
  },

  getHouseDetails(id) {
    ajax.get(URI_GETHOUSEDETAILS, {
      id: id
    }, {}, res => {
      this.setData({
        details: res.data.details,
        bannerList: res.data.banner,
      });
      WxParse.wxParse('article', 'html', res.data.details.description, this, 5);
      let update_time = this.data.details.update_time;
      let latAndLon = res.data.details.LatAndLon.split(',');
      let latAndLonInfo = {
        latitude: latAndLon[0] - 0,
        longitude: latAndLon[1] - 0,
        address: res.data.details.quxian,
        name: res.data.details.Community
      };
      this.setData({
        update_time: update_time.toString().substring(0, 10),
        latAndLon: latAndLonInfo
      });
    });
  },
  jumpToMap() {
    wx.openLocation(this.data.latAndLon)
  },
  tojisuanqi() {
    wx.navigateTo({
      url: `/pages/webview/webview?totalPrice=${this.data.details.totalPrice}`
    })
  },
  onHouseClick(e) {
    let index = e.currentTarget.dataset.index;
    let urls = [];
    for (let i in this.data.bannerList) {
      urls.push(this.data.bannerList[i].url)
    }
    wx.previewImage({
      urls: urls,
      current: urls[index]
    });
  },
  hideVideo: function (e) {
    if (e.detail.current == 0) {
      this.setData({
        img_hide: ""
      });
    } else {
      this.setData({
        img_hide: "hide"
      });
    }
  },
  play: function (e) {
    var url = e.target.dataset.url;//获取视频链接
    this.setData({
      url: url,
      hide: '',
      show: true,
    });
    var videoContext = wx.createVideoContext('video');
    videoContext.play();
  },
  close: function (e) {
    if (e.target.id == 'video') {
      return true;
    }
    this.setData({
      hide: "hide",
      show: false
    });
    var videoContext = wx.createVideoContext('video');
    videoContext.pause();
  },
  callTel() {
    wx.makePhoneCall({
      phoneNumber: '18971966446' //仅为示例，并非真实的电话号码
    })
  },
  addHistory(id){
    ajax.get(URI_ADDHISTORY,{
      lookHouseid:id
    },{},res=>{
      // console.log(res);
    })
  },
  isCollection(id){
    ajax.get(URI_ISCOLLECTION,{
      lookHouseid:id
    },{},res=>{
      this.setData({
        is_no_collection:false
      });
    })
  },
  collectionClick(){
    if(this.data.is_no_collection){
      ajax.get(URI_ADDCOLLECTION,{
        lookHouseid:this.data.houseId
      },{},res=>{
        wx.showToast({
          title: "收藏成功！",
        });
        this.setData({
          is_no_collection:false
        });
      })
    }else {
      ajax.get(URI_CANCELCOLLECTION,{
        lookHouseid:this.data.houseId
      },{},res=>{
        wx.showToast({
          title: "取消收藏",
        });
        this.setData({
          is_no_collection:true
        });
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

    return {
      title: this.data.title,
      path: `/pages/houseDetail/houseDetails?id=${this.data.houseId}&title=${this.data.title}`,
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (err) {
        console.log('转发失败')
      }
    }

  }
})